package com.github.maojx0630.onebot.socket;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.springframework.http.server.ServerHttpRequest;

import java.util.List;

/**
 * @author 毛家兴
 * @since 2024-04-02 10:39
 */
@Data
public class WsAuth {

  private String qq;

  private String token;

  /**
   * true表示都不为空 任意为空则为false;
   */
  private boolean auth;

  public WsAuth(ServerHttpRequest request) {
    {
      List<String> list = request.getHeaders().get("X-Self-ID");
      if (CollUtil.isNotEmpty(list)) {
        String first = list.get(0);
        if (StrUtil.isNotBlank(first)) {
          qq = first;
        }
      }
    }
    {
      List<String> list = request.getHeaders().get("authorization");
      if (CollUtil.isNotEmpty(list)) {
        String first = list.get(0);
        if (StrUtil.isNotBlank(first)) {
          token = StrUtil.removePrefix(first, "Bearer").trim();
        }
      }
    }

    this.auth = !StrUtil.hasBlank(this.qq, this.token);
  }
}
